package com.alan.springBoot.task;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * 1 在启动类上加 @EnableScheduling
 *      SpringBoot定时任务默认单线程，多线程需要自行实现或配置文件
 *      单线程执行定时任务（串行任务）
 * 2 xml配置多线程的
 *      <task:executor id="executor" pool-size="5" />
 *      <task:scheduler id="scheduler" pool-size="10" />
 *      <task:annotation-driven executor="executor" scheduler="scheduler" />
 * cron等也可以通过xml进行设置如下：
 *  <task:scheduled-tasks scheduler="scheduler">
 *    <task:scheduled ref="orderReportJob" method="hourReportTask" cron="0 0 0/2 * * ?"/>
 *    <task:scheduled ref="orderReportJob" method="dayReportTask" cron="0 0 2 * * ?"/>
 *  </task:scheduled-tasks>
 *
 * Created by Alan on 17-9-26
 */
@Component
public class ScheduledTest {

    private static final Logger logger = LoggerFactory.getLogger(ScheduledTest.class);

    @Scheduled(cron="0 0/2 8-20 * * ?")
    public void executeFileDownLoadTask() {

        // 间隔2分钟,执行工单上传任务
        Thread current = Thread.currentThread();
        System.out.println("Test-定时任务1:"+current.getId());
        logger.info("ScheduledTest.executeFileDownLoadTask 定时任务1:"+current.getId()+ ",name:"+current.getName());
    }

    @Scheduled(cron="0 0/1 8-20 * * ?")
    public void executeUploadTask() {

        // 间隔1分钟,执行工单上传任务
        Thread current = Thread.currentThread();
        System.out.println("Test-定时任务2:"+current.getId());
        logger.info("ScheduledTest.executeUploadTask 定时任务2:"+current.getId() + ",name:"+current.getName());
    }

    @Scheduled(cron="0 0/3 5-23 * * ?")
    public void executeUploadBackTask() {

        // 间隔3分钟,执行工单上传任务
        Thread current = Thread.currentThread();
        System.out.println("Test-定时任务3:"+current.getId());
        logger.info("ScheduledTest.executeUploadBackTask 定时任务3:"+current.getId()+ ",name:"+current.getName());
    }


}
